﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DukeNUS.FacilitiesPlanning.FPSystem.Models
{
    public class ActionResponse
    {

        public ErrorResponse[] GetErrors(ModelStateDictionary modelState)
        {
            var errorResponses = new List<ErrorResponse>();

            if (!modelState.IsValid)
            {
                var modelStateValues = modelState.Values.ToList();
                var modelStateKeys = modelState.Keys.ToList();

                for (int i = 0; i < modelStateKeys.Count; i++)
                {
                    if (modelStateValues[i].Errors.Count > 0) errorResponses.Add(new ErrorResponse()
                                                                {
                                                                    For = modelStateKeys[i],
                                                                    Message = modelStateValues[i].Errors[0].ErrorMessage 
                                                                });
                }
            }

            return errorResponses.ToArray();
        }

    }
}